package com.lkd.controller;

import com.lkd.dto.RegionDto;
import com.lkd.entity.Node;
import com.lkd.service.INodeService;
import com.lkd.vo.NodeDetailVo;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/vm-service/node")
public class NodeController {

    private final INodeService nodeService;

    @GetMapping("/search")
    public PageBean<NodeVo> searchNode(  RegionDto regionDto) {
        PageBean<NodeVo> page = nodeService.search(regionDto);
        return page;
    }

    @PostMapping
    public void addNode(@RequestBody Node node) {
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        nodeService.save(node);
    }

    @GetMapping("/vmList/{nodeId}")
    public NodeDetailVo getVmList(@PathVariable("nodeId") Long nodeId) {
        NodeDetailVo list = nodeService.getVmList(nodeId);
        return list;
    }

    @PutMapping
    public void updateNode(@RequestBody Node node) {
        node.setUpdateTime(LocalDateTime.now());
        nodeService.updateById(node);
    }

    @DeleteMapping("/{nodeId}")
    public void deleteNode(@PathVariable Long nodeId) {
        nodeService.removeById(nodeId);
    }

    @GetMapping("count")
    public Result count() {
        return Result.success(nodeService.count());
    }





}
